﻿using System;
using System.Collections.Generic;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using System.Data;
using System.Data.SqlClient;

namespace SDLITS.Web.SystemManage
{
    public partial class CardModifyDetail : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    LoadInfo();
                }
                else
                {
                    Response.Redirect("../default.aspx");
                }
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            string birthday = txt_birth.Text.Trim();
            if (string.IsNullOrEmpty(birthday))
            {
                birthday = "1900-01-01";
            }
            SqlParameter[] sp2 = new SqlParameter[]{
                    new SqlParameter("@MemberID",Convert.ToInt32(ViewState["MemberID"])),
                    new SqlParameter("@IDNumber",txt_sfz.Text.Trim()),
                    new SqlParameter("@Name",txt_name.Text),
                    new SqlParameter("@Sex", Convert.ToBoolean(txt_sex.Text.Equals("男"))),
                    new SqlParameter("@Birthday",Convert.ToDateTime(birthday)),
                    new SqlParameter("@IDAddress",txt_addr.Text),
                    new SqlParameter("@Cell",txt_cell.Text),
                    new SqlParameter("@Phone",txt_phone.Text),
                    new SqlParameter("@DLName",txt_drivername.Text),
                    new SqlParameter("@DLSex",rblsex.SelectedValue == "1" ? true : false),
                    new SqlParameter("@DLNumber",txt_cardno.Text),
                    new SqlParameter("@DLAddress",txt_cardaddr.Text),
                    new SqlParameter("@CarNo",txt_carno.Text),
                    new SqlParameter("@CarEnginNo", txt_fdj.Text),
                    new SqlParameter("@CarIDNo",txt_bs.Text),
                    new SqlParameter("@CarOwnerName",txt_cz.Text),
                    new SqlParameter("@CarOwnerAddr",txt_czdz.Text),
                    new SqlParameter("@GCarNo",txt_gchm.Text),
                    new SqlParameter("@GCarIDNo",txt_gcbs.Text),
                    new SqlParameter("@MemCardID", Convert.ToInt32(ViewState["MemCardID"])),
                    new SqlParameter("@Custom1",txt_note1.Text),
                    new SqlParameter("@Custom2",txt_note2.Text),
                    new SqlParameter("@SPAction","UpdateByIDNumber")
                };
            SDDbSession.Default.FromProc("TableMemberInfo").AddParameter(sp2).ExecuteNonQuery();
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }

        protected void LoadInfo()
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                V_MemCardInfoType card = SDDbSession.Default.From<V_MemCardInfoType>().Where(V_MemCardInfoType._.MemCardNo == Request["id"]).ToFirst();
                if (card != null)
                {
                    ViewState["MemCardID"] = card.MemCardID.ToString();
                    ViewState["MemberID"] = card.MemberID.ToString();

                    #region 1
                    txt_sfz.Text = card.IDNumber;
                    txt_name.Text = card.Name;
                    txt_addr.Text = card.IDAddress;
                    txt_sex.Text = card.Sex.ToString().ToLower() == "false" ? "女" : "男";
                    txt_birth.Text = card.Birthday.ToString();
                    txt_phone.Text = card.Phone;
                    txt_cell.Text = card.Cell;
                    txt_note1.Text = card.Custom1;
                    txt_note2.Text = card.Custom2;
                    img1.ImageUrl = string.Format("{0}{1}.bmp", @"/Img/IDShot/", card.IDNumber);
                    Image1.ImageUrl = string.Format("{0}{1}.bmp", @"/Img/FingerShot/", card.IDNumber);
                    Image2.ImageUrl = string.Format("{0}{1}.jpg", @"/Img/CamShot/", card.IDNumber);
                    #endregion
                    #region 2
                    lblStartDate.Text = card.StartDate.ToString();
                    lblExpireDate.Text = card.ExpireDate.ToString();
                    lblRegistID.Text = card.RegistID;
                    lblMemCardNo.Text = card.MemCardNo;
                    lblLeftCash.Text = card.LeftCash.ToString();
                    lblMemType.Text = card.MemType;
                    #endregion
                    #region 3
                    txt_drivername.Text = card.DLName;
                    rblsex.SelectedValue = string.IsNullOrEmpty(card.DLSex.ToString()) ? "1" : (Boolean.Parse(card.DLSex.ToString()) ? "1" : "2");
                    txt_cardno.Text = card.DLNumber;
                    txt_cardaddr.Text = card.DLAddress;
                    #endregion
                    #region 4
                    txt_carno.Text = card.CarNo;
                    txt_fdj.Text = card.CarEnginNo;
                    txt_bs.Text = card.CarIDNo;
                    txt_cz.Text = card.CarOwnerName;
                    txt_czdz.Text = card.CarOwnerAddr;
                    txt_gchm.Text = card.GCarNo;
                    txt_gcbs.Text = card.GCarIDNo;
                    #endregion
                }
                else
                {
                    PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
                    Alert.Show("没有该卡号信息");
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SetPrintSession();
            PageContext.RegisterStartupScript(Window1.GetShowReference("CardModifyPrint.aspx", "打印交割单"));
        }
        private void SetPrintSession()
        {

            var mp = new ModelPrint
            {
                RegistID = lblRegistID.Text,
                MemCardNo = lblMemCardNo.Text,
                StartDate = lblStartDate.Text,
                ExpireDate = lblExpireDate.Text,
                IDNumber = txt_sfz.Text,
                Name = txt_name.Text,
                Address = txt_addr.Text,
                Sex = txt_sex.Text,
                Birthday = txt_birth.Text,
                Cell = txt_cell.Text,
                Phone = txt_phone.Text,
                LeftCash = lblLeftCash.Text,
                DLName = txt_drivername.Text,
                DLSex = rblsex.SelectedValue,
                DLNumber = txt_cardno.Text,
                DLAddress = txt_cardaddr.Text,
                CarNo = txt_carno.Text,
                CarEnginNo = txt_fdj.Text,
                CarIDNo = txt_bs.Text,
                CarOwnerName = txt_cz.Text,
                CarOwnerAddr = txt_czdz.Text,
                GCarNo = txt_gchm.Text,
                GCarIDNo = txt_gcbs.Text,
                PrintTime = DateTime.Now.ToString()
            };
            Session["ModelPrint"] = mp;
        }
    }
    public class ModelPrint
    {
        public string MemCardID { get; set; }
        public string MemCardNo { get; set; }
        public string RegistID { get; set; }
        public string StartDate { get; set; }
        public string ExpireDate { get; set; }
        public string IDNumber { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Sex { get; set; }
        public string Birthday { get; set; }
        public string Cell { get; set; }
        public string Phone { get; set; }
        public string LeftCash { get; set; }
        public string LeftTimes { get; set; }
        public string DLName { get; set; }
        public string DLSex { get; set; }
        public string DLNumber { get; set; }
        public string DLAddress { get; set; }
        public string CarNo { get; set; }
        public string CarEnginNo { get; set; }
        public string CarIDNo { get; set; }
        public string CarOwnerName { get; set; }
        public string CarOwnerAddr { get; set; }
        public string GCarNo { get; set; }
        public string GCarIDNo { get; set; }
        public string CompanyName { get; set; }
        public string Room { get; set; }
        public string ContactPerson { get; set; }
        public string CompanyPhone { get; set; }
        public string StartPlace { get; set; }
        public string EndPlace { get; set; }
        public string GoodName { get; set; }
        public string AmountUnit { get; set; }
        public string CarInfo { get; set; }
        public string TransPrice { get; set; }
        public string PrintTime { get; set; }

        public string CompanyIDNumber { get; set; }
    }
}